﻿using System.Collections.Generic;
using System.Threading.Tasks;

namespace Odyssey.Consul.Implementations
{
    using Features;
    using Http;
    using Models;
    public class Connect : BaseConsulAPI, IConnect
    {
        public Connect(IHttpRequester httpRequester, IConsulConfiguration consulConfiguration) : base(httpRequester, consulConfiguration, EnumResource.Operator)
        {

        }

        #region Certificate Authority
        private string BuildCAPath(string action = "")
        {
            return BuildUri("connect/ca", action);
        }
        public async Task<ResultCAConfiguration> CAGetConfiguration()
        {
            return await ApiSend<ResultCAConfiguration>(BuildCAPath("configuration"));
        }

        public async Task<ResultConnectionCARootList> CARootList()
        {
            return await ApiSend<ResultConnectionCARootList>(BuildCAPath("roots"));
        }

        public async Task CAUpdateConfiguration(ArguUpdateCAConfiguration arguUpdateCAConfiguration)
        {
            await ApiSendResponse(BuildCAPath("configuration"), arguUpdateCAConfiguration, EnumConsulHttpMethod.Put);
        }
        #endregion

        #region Intention
        private string BuildIntentionPath(string action = "")
        {
            return BuildUri("connect/intentions", action);
        }

        public async Task<ResultIntentionCheck> IntentionCheck(ArguIntentionCheck arguIntentionCheck)
        {
            QueryParameter queryParameter = null;
            if (arguIntentionCheck != null)
            {
                queryParameter = new QueryParameter();
                if (!string.IsNullOrWhiteSpace(arguIntentionCheck.Source))
                {
                    queryParameter.OtherParameters.Add("source", arguIntentionCheck.Source);
                }
                if (!string.IsNullOrWhiteSpace(arguIntentionCheck.Source))
                {
                    queryParameter.OtherParameters.Add("destination", arguIntentionCheck.Destination); ;
                }
            }
            return await ApiSend<ResultIntentionCheck>(BuildIntentionPath("check"), queryParameter: queryParameter, requestHeaders: BuildTokenHeader(EnumResource.Intentions));
        }

        public async Task<ResultIntentionCheck> IntentionCreate(ArguIntention arguIntention)
        {
            return await ApiSend<ArguIntention, ResultIntentionCheck>(BuildIntentionPath(), arguIntention, EnumConsulHttpMethod.Post, requestHeaders: BuildTokenHeader(EnumResource.Intentions));
        }

        public async Task IntentionDelete(string id)
        {
            await ApiSendResponse(BuildIntentionPath(id), EnumConsulHttpMethod.Delete, requestHeaders: BuildTokenHeader(EnumResource.Intentions));
        }

        public async Task<List<ResultIntention>> IntentionList()
        {
            return await ApiSend<List<ResultIntention>>(BuildIntentionPath(), requestHeaders: BuildTokenHeader(EnumResource.Intentions));
        }

        public async Task<ResultIntentionMatchList> IntentionMatchList(ArguIntentionMatchList arguIntentionMatchList)
        {
            QueryParameter queryParameter = null;
            if (arguIntentionMatchList != null)
            {
                queryParameter = new QueryParameter();
                if (!string.IsNullOrWhiteSpace(arguIntentionMatchList.By))
                {
                    queryParameter.OtherParameters.Add("by", arguIntentionMatchList.By);
                }
                if (!string.IsNullOrWhiteSpace(arguIntentionMatchList.Name))
                {
                    queryParameter.OtherParameters.Add("name", arguIntentionMatchList.Name); ;
                }
            }
            return await ApiSend<ResultIntentionMatchList>(BuildIntentionPath("check"), queryParameter: queryParameter, requestHeaders: BuildTokenHeader(EnumResource.Intentions));
        }

        public async Task<ResultIntention> IntentionRead(string id)
        {
            var path = BuildIntentionPath(id);
            return await ApiSend<ResultIntention>(path, requestHeaders: BuildTokenHeader(EnumResource.Intentions));
        }

        public async Task IntentionUpdate(ArguIntention arguIntention)
        {
            await ApiSendResponse(BuildIntentionPath(arguIntention.ID), arguIntention, EnumConsulHttpMethod.Put, requestHeaders: BuildTokenHeader(EnumResource.Intentions));
        }
        #endregion
    }
}
